﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace BfLib.Web.RSSLite
{
    public class RSSLiteChannel
    {
        private List<RSSLiteItem> _rssItems;

        //Mandatory channel elements
        private string _title, _link, _description, _image;

        public string Image
        {
            get { return _image; }
        }




        #region Public accessors

        public string Description
        {
            get { return _description; }
        }

        public string Link
        {
            get { return _link; }
        }

        public string Title
        {
            get { return _title; }
        }

   

        #endregion

        public RSSLiteChannel(XmlNode node)
        {
            RSSLiteItem rssItem;
            _rssItems = new List<RSSLiteItem>();

            XmlNode n;
            XmlNodeList nodes;

            #region Parse channel elements

            n = node.SelectSingleNode("title");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <title> element");
            else
                this._title = n.InnerXml;

            n = node.SelectSingleNode("link");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <link> element");
            else
                this._link = n.InnerXml;

            n = node.SelectSingleNode("description");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <description> element");
            else
                this._description = n.InnerXml;

            n = node.SelectSingleNode("image");
            if (n != null)
                this._image = n.InnerXml;
            


            #endregion

            nodes = node.SelectNodes("item");

            if (nodes == null)
                return;
            if (nodes.Count == 0)
                return;

            foreach (XmlNode nd in nodes)
            {
                rssItem = new RSSLiteItem(nd);
                _rssItems.Add(rssItem);
            }
        }

        public List<RSSLiteItem> RSSLiteItems
        {
            get
            {
                return _rssItems;
            }
        }
    }

}
